const userModel = require('../models/userModel')
const roleModel = require('../models/roleModel')
const {getMd5Password} = require('../utils/encryption')
const {Op} = require('sequelize')
const {searchPageSize} = require('../config/config')

module.exports ={
    getAllUsersBySearch:async(userNum,username,userrole) =>{
        let whereObj = {}
        userNum && Object.assign(whereObj,{
            userNum:{
                [Op.like]:`%${userNum}%`
            }
        })
        username && Object.assign(whereObj,{
            userName:{
                [Op.like]:`%${username}%`
            }          
        })
        // ssex && Object.assign(whereObj,{sex:ssex})
        userrole && Object.assign(whereObj,{roleId:userrole})
        // console.log(whereObj);
        try{
          let count = await userModel.count({where:whereObj});
          console.log(count);
          let users;
          if(count !==0){
            users = await userModel.findAll({
                attributes:{
                    exclude: ["salt","password"],
                },
                order: [['userNum','ASC']],
                include:[{
                    model:roleModel
                }],
                where:whereObj,
                // limit:searchPageSize,
                // offset:(pageIndex-1) * searchPageSize
            })
          }
          console.log(users)
          return {users:users} || null
        //   return {users:users,pageCount:count} || null
        }catch(error){
          console.log("find all users error:",error);
          return false
        }
    },
    getAllRoles:async() =>{
        let whereObj = {}
        try{
            let roles = await roleModel.findAll({
                //   attributes:{
                //       exclude: ["roleLevel"],
                //   },
                  where:whereObj,
              })
            return roles
          }catch(error){
            console.log("find all roles error:",error);
            return false
          }
    },
    getUserbyUserNo:async(userNum) =>{
        try{
            let result = await userModel.findOne({
                where:{
                    userNum
                },
                include:[{
                    model:roleModel
                }],
            });                  
              return result
        }catch(error){
            console.error('find user by userNum error:'+error);
            return false
        }
    },
    getUserInfobyCheckLogin:async(userNum,inputPassword) =>{
        try{
            let result = await userModel.findOne({
                where:{
                    userNum
                },
                include:[{
                    model:roleModel
                }]
            });
            if(result){
                let checkPassword = await getMd5Password(inputPassword,result.salt)
                console.log("加密后的输进来的密码:   "+checkPassword)
                console.log("数据库里的密码:   "+result.password);
                if(result.password == checkPassword){
                    result.salt = '',
                    result.password = ''
                    return result
                }else{
                    return null
                }
            }else{
                return null
            }
        }catch(error){
            console.error('find user by userNum error:'+error);
            return false
        }
    },
    addUser:async(userNum,userName,sex,roleId) =>{
        try{
            let result = await userModel.create({
                userName,
                userNum,
                password:'13a9fc507ae77467e8a1d3331df23849',
                sex,
                roleId,
                salt:'9840d93a-da7c-4e2c-ac0e-1185ca246200'
            })
          console.log(result)
          return result
        }catch(error){
            console.error('add user error:'+error);
            return error
        }
    },
    deleteUserbyUserNo:async(userNum) =>{
        try{
            let result = await userModel.destroy({
                where:{
                    userNum
                }
            })
            return result
        }catch(error){
            console.error('delete user by userNum error:'+error);
            return error
        }
    },
    updateUserbyUserNo:async(userNum,userName,sex,roleId) =>{
        try{
            let result = await userModel.findOne({
                where:{
                    userNum
                }
            }).then((emp) =>{
                emp.update({
                    userName,
                    sex,
                    roleId
                })
                return emp
            })
            return result
        }catch(error){
            console.error('update user by userNum error:'+error);
            return error
        }
    },
}